﻿
CREATE PROCEDURE [dbo].[CashRegisterLinesInsert]
(
	@DocId int,
	@PartyId int,
	@Qntty int,
	@RetailSum decimal(19, 2),
	@DiscountValue tinyint, --значение скидки по строке чека
	@Note varchar(250),
	@MD datetime,
	@MW varchar(50)
)
AS
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int
DECLARE @intDocTypeId int, @intNextId int, @intSaleDiscountTypesId int --тип скидки
DECLARE @intReturnedQntty int, @intRestQntty int
DECLARE @strBarCode char(12), @bitBlocked bit, @decPartyPrice decimal(19, 2)
	SET NOCOUNT ON

IF (@DocId Is Null) OR (@PartyId Is Null) OR (@Qntty Is Null) OR (@RetailSum Is Null)
BEGIN
	SELECT @intErrCode = 1, @strMsg = 'Неправильные параметры в вызове процедуры ' + Object_Name(@@Procid) + ' !'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF (@Qntty <=0) OR (@RetailSum <=0)
BEGIN
	SELECT @intErrCode = 2, @strMsg = 'Количество и сумма д.б. больше нуля !'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

SELECT @intDocTypeId = DocTypeId, @intSaleDiscountTypesId = ShiftDepartmentId 
FROM [CA].[Documents] WHERE Id = @DocId
IF (@intDocTypeId Is Null)
BEGIN
	SELECT @intErrCode = 3, @strMsg = 'Не найден документ с идентификатором ' + cast(@DocId as varchar(20)) + ' !'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF (@intDocTypeId <> 3) --кассовый чек
BEGIN
	SELECT @intErrCode = 4, @strMsg = 'Неправильный тип документа с идентификатором ' + cast(@DocId as varchar(20)) + ' !'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

SELECT @bitBlocked = Blocked, @intRestQntty = RestQntty, @strBarCode = BarCode, @decPartyPrice = RetailPrice
FROM CD.Parties WHERE Id = @PartyId

IF @bitBlocked = 1
BEGIN
	SELECT @intErrCode = 5, 
	@strMsg = 'Партия ' + @strBarCode + '(' + cast(@PartyId as varchar(20)) + ') заблокирована для продажи!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF @Qntty > @intRestQntty
BEGIN
	SELECT @intErrCode = 6, 
	@strMsg = 'Остаток по партии ' + @strBarCode + '(' + cast(@PartyId as varchar(20)) + ') меньше введённого количества!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF (@DiscountValue Is Not Null) AND (@intSaleDiscountTypesId Is Null) 
BEGIN
	SELECT @intErrCode = 7, 
	@strMsg = 'Чек создан без указания типа скидки!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

SELECT @decPartyPrice = @decPartyPrice * (100.0 - IsNull(@DiscountValue, 0) ) * @Qntty / 100.0  --сумма со скидкой
IF @decPartyPrice <> @RetailSum
BEGIN
	SELECT @intErrCode = 8, 
	@strMsg = 'Неправильная сумма по строке чека!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

SELECT @Qntty = @Qntty * (-1), @RetailSum = @RetailSum * (-1.0)

EXEC [dbo].[ProcessStatistic] @Note OUTPUT, @MD OUTPUT, @MW OUTPUT
--получение значения идентификатора
EXEC @intRet = dbo.GetNextCounterFor 'GoodsLines', @intNextId OUTPUT
IF @intRet <> 0 RETURN @intRet

	SET NOCOUNT OFF;
INSERT INTO [CA].[GoodsDocLines] (Id, [DocId], [PartyId], [Qntty], [RetailSum], DiscountValue, [Note], [MD], [MW]) 
VALUES ( @intNextId, @DocId, @PartyId, @Qntty, @RetailSum, @DiscountValue, @Note, @MD, @MW);
	
SELECT Id, DepartmentId, DocId, PartyId, 
Qntty * (-1) AS Qntty, 
RetailSum * (-1.0) AS RetailSum, DiscountValue,
Note, MD, MW 
FROM CA.GoodsDocLines
WHERE Id = @intNextId
RETURN 0
